﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web.Script.Serialization;


namespace SAM_Edu.api
{
    public class OTP
    {
        public SmsResponse SendSMS(string apikey, string apisecret, string to, string text)
        {
            string uri = string.Format("http://rest.nexmo.com/sms/json?api_key={0}&api_secret={1}&from={2}&to={3}&text={4}", apikey, apisecret, "6596658673", to, text);
            var json = new WebClient().DownloadString(uri);
            return ParseSmsResponseJson(json);
        }

        private SmsResponse ParseSmsResponseJson(string json)
        {
            // hyphens are not allowed in in .NET var names
            json = json.Replace("message-count", "MessageCount");
            json = json.Replace("message-price", "MessagePrice");
            json = json.Replace("message-id", "MessageId");
            json = json.Replace("remaining-balance", "RemainingBalance");
            return new JavaScriptSerializer().Deserialize<SmsResponse>(json);
        }
    }
    public class Message
    {
        public string to;
        public string messageprice;
        public string status;
        public string messageId;
        public string remainingBalance;
        public string To
        {
            get
            {
                return this.to;
            }
            set
            {
                this.to = value;
            }
        }
        public string Messageprice
        {
            get
            {
                return this.messageprice;
            }
            set
            {
                this.messageprice = value;
            }
        }
        public string Status
        {
            get
            {
                return this.status;
            }
            set
            {
                this.status = value;
            }
        }
        public string MessageId
        {
            get
            {
                return this.messageId;
            }
            set
            {
                this.messageId = value;
            }
        }
        public string Remainingbalance
        {
            get
            {
                return this.remainingBalance;
            }
            set
            {
                this.remainingBalance = value;
            }
        }
    }

    public class SmsResponse
    {
        public string messagecount;
        public List<Message> messages;
        public string Messagecount
        {
            get
            {
                return this.messagecount;
            }
            set
            {
                this.messagecount = value;
            }
        }
        public List<Message> Messages
        {
            get
            {
                return this.messages;
            }
            set
            {
                this.messages = value;
            }
        }
    }
}
